package com.tianji.learning.mq.listeners;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.QA_LIKED_TIMES_KEY;

@Slf4j
@Component
@RequiredArgsConstructor
public class RelayLikedTImesChangeListener {
    private final IInteractionReplyService replyService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "reply.like.times.change.queue", durable = "true"),
            exchange = @Exchange(name = LIKE_RECORD_EXCHANGE, type = "topic"),
            key = QA_LIKED_TIMES_KEY
    ))
    public void replyLikedTimesChange(List<LikedTimesDTO> likedTimesList) {
        log.info("收到点赞次数变更消息：{}", likedTimesList);
        //1、循环遍历接收收到的消息，封装到po集合中，批量存入
        List<InteractionReply> replyList = new ArrayList<>(likedTimesList.size());
        for (LikedTimesDTO likedTimesDTO : likedTimesList) {
            replyList.add(new InteractionReply()
                    .setId(likedTimesDTO.getBizId())
                    .setLikedTimes(likedTimesDTO.getLikedTimes()));
        }
        replyService.updateBatchById(replyList);
    }

}
